<template>
  <div class="hyydxq">
    <!-- 预约信息 -->
    <van-search
      clearable
      background="transparent"
      v-model="params.enterpriseName"
      class="b-b-2"
      placeholder="搜索企业名称"
      @search="onSearch"
    />
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <van-radio-group @change="onChanges" v-model="radio">
          <van-cell v-for="(item, index) in list" clickable :key="index" class="approvalItem content">
            <div class="flex-between font-13 line-height-3">
              <div class="col-666666 m-r-10 line-text-out" style='width: 90%'>
                <p>{{ item.enterpriseName }}</p>
                <p>联系人：{{item.contactPerson}}</p>
              </div>
            </div>
            <template #right-icon>
              <van-radio checked-color="#37B5B5" :name="item.id" class="flex-end"></van-radio>
            </template>
          </van-cell>
<!--          <van-cell v-for="(item, key) in list" :key="key" class="approvalItem content">-->
<!--            <div class="flex-between font-14 line-height-3">-->
<!--              <div class="col-333333 font-bold-500 font-15 m-r-10">{{ item.enterpriseName }}</div>-->
<!--            </div>-->
<!--            <van-radio checked-color="#37B5B5" :name="item.id" class="flex-end"></van-radio>-->
<!--            <div class="flex-between font-13 line-height-3">-->
<!--              <div class="col-666666 m-r-10 line-text-out">-->
<!--              <p>联系人：{{item.contactPerson}}</p> -->
<!--              </div>-->
<!--            </div>-->
<!--          </van-cell>-->
        </van-radio-group>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getValue, setValue } from '@/utils/auth'
import {
  Toast,
  Cell,
  CellGroup,
  Popup,
  Picker,
  DatetimePicker,
  PullRefresh,
  List,
  Search,
  RadioGroup,
  Radio
} from 'vant'
import { getHandle} from '@/api'
export default {
  components: {
    [Cell.name]: Cell,
    [CellGroup.name]: CellGroup,
    [Popup.name]: Popup,
    [Picker.name]: Picker,
    [DatetimePicker.name]: DatetimePicker,
    [PullRefresh.name]: PullRefresh,
    [Toast.name]: Toast,
    [List.name]: List,
    [Search.name]: Search,
    [RadioGroup.name]: RadioGroup,
    [Radio.name]: Radio
  },
  data() {
    return {
      radio: null,
      keyword: '',
      refreshing: false,
      list: [],
      loading: false,
      finished: false,
      params: {
        page: 1,
        perPage: 10,
        subjectInfoId: getValue('subjectInfoId'),
        tenantCode: getValue('tenantCode'),
        keyword: null
      }
    }
  },
  created() {
    const handle = getValue('milk-object-handle')
    if (handle === 'chooseEnterprise') {
      const obj = JSON.parse(getValue('milk-object'))
      this.radio = obj.whereaboutsEnterpriseId
    }
  },
  mounted() {},
  methods: {
    // 卡片列表
    onRefresh() {
      this.params.page = 1
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
    },
    onSearch() {
      this.params.page = 1
      this.list = []
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
    },
    onLoad() {
      getHandle(`/agri/api/common/subjectInfoEnterprise/queryMachiningOrMilkStationListPage`, this.params).then(res => {
        if (this.refreshing) {
          this.list = []
          this.refreshing = false
        }
        this.list = this.list.concat(res.data.rows)
        this.loading = false
        if (this.list.length >= res.data.count) {
          this.finished = true
        } else {
          this.params.page = this.params.page + 1
        }
      })
    },
    onChanges(val) {
     setValue('enterprise-obj', JSON.stringify(this.list.find(item => item.id === val)))
      this.$bridge.call('base.closePage')
    }
  }
}
</script>

<style lang="scss" scoped>
.topCeiling {
  position: fixed;
  top: 0;
  z-index: 1;
}
.van-field__control {
  border: 0 !important;
}
.content {
  //   border-bottom: 1px solid #eee;
  padding: 10px 15px;
}
::v-deep .van-search {
  width: 98%;
  margin: auto;
}
::v-deep .van-search__content {
  background-color: #fff;
  border-radius: 34px;
}
</style>
